﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Controllers
{
	using System.Web.Mvc;
	using Falcon.Core.Domain;
	using Falcon.Core.Domain.Enums;
	using Falcon.Core.Domain.Services;
	using Falcon.Core.Domain.Values;
	using Falcon.Core.Extensions;
	using Falcon.Core.Validation;

	[Authorize]
	public class UserContactController : DefaultController
	{
		public UserContactController(IUserContactService userContactService)
		{
			this.UserContactService = userContactService;
		}

		public IUserContactService UserContactService
		{
			get; 
			set;
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Index(string mode, int? page)
		{
			var contacts = UserContactService.GetPaged(new UserContactSpecification
			{
				UserId = Identity.Id,
				Mode = mode.AsEnum<UserContactViewMode>(),
				Page = page,
				Limit = Settings.Users.ContactPageLimit
			});

			var contact = contacts.FirstOrDefault();

			return contact.CanView ? View(Views.Index, contacts) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Ignore()
		{
			var contact = UserContactService.Create();

			var value = new UserContactCreate
			{
				Ignore = true
			};

			return contact.CanCreate ? View(Views.Ignore, value) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Create()
		{
			var contact = UserContactService.Create();

			return contact.CanCreate ? View(Views.Create, new UserContactCreate()) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Post), ValidateAntiForgeryToken]
		public ActionResult Create(UserContactCreate value)
		{
			try
			{
				var contact = UserContactService.Create();

				if (!contact.CanCreate)
				{
					return NotAuthorized();
				}

				UserContactService.Insert(contact, value);

				return RedirectToRoute(Routes.UserContactIndex);
			}
			catch (ValidationException exception)
			{
				ModelState.CopyValidationExceptions(exception);
			}

			return View((value.Ignore) ? Views.Ignore : Views.Create, value);
		}

		[AcceptVerbs(HttpVerbs.Post), ValidateAntiForgeryToken]
		public ActionResult Update(UserContactUpdate[] values, UserContactAction value)
		{
			if (values != null && values.Length > 0)
			{
				for (var i = 0; i < values.Length; i++)
				{
					if (values[i] == null || !values[i].Selected)
					{
						continue;
					}

					var contact = UserContactService.GetById(values[i].Id);

					if (contact == null)
					{
						return Error(Resources.Messages.UserContactNotFound);
					}

					if (!contact.CanUpdate)
					{
						return NotAuthorized();
					}

					if (value == UserContactAction.Delete)
					{
						UserContactService.Delete(contact);
					}
					else
					{
						UserContactService.Update(contact, value);
					}
				}
			}

			return RedirectToRoute(Routes.UserContactIndex);
		}


		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Friend(int id, string answer)
		{
			var contact = UserContactService.GetById(id);

			if (contact == null)
			{
				return Error(Resources.Messages.UserContactNotFound);
			}

			if (!contact.CanUpdate)
			{
				return NotAuthorized();
			}

			UserContactService.Update(contact, answer.AsEnum<UserContactFriendAction>());

			return RedirectToRoute(Routes.UserContactIndex, new { mode = "default" });
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Delete(int id)
		{
			var contact = UserContactService.GetById(id);

			if (contact == null)
			{
				return Error(Resources.Messages.UserContactNotFound);
			}

			if (!contact.CanDelete)
			{
				return NotAuthorized();
			}

			UserContactService.Delete(contact);

			return RedirectToRoute(Routes.UserContactIndex);
		}
	}
}